package com.rapida.theme;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import java.util.ArrayList;
import java.util.List;

public class ThemeManager {
	private static ThemeManager mInst;
	private Context mContext;
	private PackageManager mPackageManager;
	private String mThemePackage;
	private ArrayList<ThemeInfo> mThemes = new ArrayList<ThemeInfo>();

	public static ThemeManager get(Context context) {
		if (mInst == null) {
			mInst = new ThemeManager(context);
		}
		return mInst;
	}

	private ThemeManager(Context context) {
		mContext = context;
		mThemePackage = mContext.getPackageName() + ".";
		mPackageManager = mContext.getPackageManager();
	}

	public void searchTheme() {
		List<PackageInfo> packages = mPackageManager.getInstalledPackages(PackageManager.PERMISSION_GRANTED);
		mThemes.clear();
		for (PackageInfo info : packages) {
			if (info.packageName.startsWith(mThemePackage)) {
				ThemeInfo theme = new ThemeInfo();
				theme.packageName = info.packageName;
				theme.versionCode = info.versionCode;
				theme.versionName = info.versionName;
				theme.themeName = info.applicationInfo.loadLabel(mPackageManager).toString();
				theme.themeIcon = info.applicationInfo.loadIcon(mPackageManager);
				mThemes.add(theme);
			}
		}
	}

	public ArrayList<ThemeInfo> getThemes() {
		return mThemes;
	}
}
